﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using Kaleida.ServiceMonitor.Model;

namespace Kaleida.ServiceMonitor.UI.NotificationPanels
{
    public partial class TenFootNotificationPanel : UserControl, INotificationPanel
    {
        private IEnumerable<ScriptSourceMenuItem> scriptMenuItems = Enumerable.Empty<ScriptSourceMenuItem>();
        private Monitor monitor;

        public TenFootNotificationPanel()
        {
            InitializeComponent();
        }

        public void Initialise(Monitor monitorApplication)
        {
            monitor = monitorApplication;
        }

        public string PanelName
        {
            get { return "10' Summary"; }
        }

        public void RefreshContent()
        {
            var presentation = monitor.State.DefaultPresentation;

            BackColor = presentation.GetNotificationColour();
            lbl10FootSymbol.Text = presentation.GetNotificationWord();
            lbl10FootDetail.Text = presentation.GetStateDetail();

            lblScriptName.Text = monitor.Workspace.CurrentScript.Name;
        }

        public Control GetPanelControl()
        {
            return this;
        }

        private void TenFootNotificationPanel_Resize(object sender, EventArgs e)
        {
            var isMini = Width < 400 || Height < 200;            
            
            lblScriptName.Font = new Font(FontFamily.GenericSansSerif, isMini ? 18 : 26);
            lbl10FootSymbol.Visible = !isMini;
            lbl10FootDetail.Height = isMini ? 48 : 56;
            lbl10FootDetail.Font = isMini ? new Font("Segoe UI", 7.5f) : new Font("Courier New", 8.25f);
        }

        private void OnSelectScriptClicked(object sender, EventArgs e)
        {
            var menuItem = (ScriptSourceMenuItem)sender;

            monitor.Workspace.CurrentScript = menuItem.Script;
        }

        private void contextMenu_Opening(object sender, System.ComponentModel.CancelEventArgs e)
        {
            miStartMonitoring.Enabled = monitor.State.CanBeginMonitoring;
            miStopMonitoring.Enabled = monitor.State.IsMonitoring;
            miAcknowledgeErrors.Enabled = monitor.State.HasOperationFailures;

            var allScripts = monitor.Workspace.LoadedScripts;
            scriptMenuItems = allScripts.Select(i => new ScriptSourceMenuItem(i, OnSelectScriptClicked));
            miSelectScript.DropDownItems.Clear();
            miSelectScript.DropDownItems.AddRange(scriptMenuItems.Cast<ToolStripItem>().ToArray());
            miSelectScript.Enabled = allScripts.Any();
        }

        private void miStartMonitoring_Click(object sender, EventArgs e)
        {
            monitor.State.StartMonitoring();
        }

        private void miStopMonitoring_Click(object sender, EventArgs e)
        {
            monitor.State.StopMonitoring();
        }

        private void miAcknowledgeError_Click(object sender, EventArgs e)
        {
            monitor.AcknowledgeErrors();
        }
    }
}
